package ar.com.e2solver.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

import ar.com.e2solver.solver.SolverResult;

public class ResultView extends Panel {

	private static final long serialVersionUID = 1L;
	private SolverResult result;
	
	public ResultView() {
		initialize();
	}
	
	public ResultView(SolverResult result) {
		this.result = result;
		initialize();
	}
	
	private void initialize(){
		BorderLayout bl = new BorderLayout();
		this.setLayout(bl);
		
		Panel fieldsPanel = new Panel();
		GridLayout gl = new GridLayout(5,2);
		fieldsPanel.setLayout(gl);
		
		Label maxFitness = new Label("Mayor fitness: ");
		TextField maxFitnessField = new TextField(5);
		maxFitnessField.enableInputMethods(false);
		maxFitnessField.setEditable(false);
		if(result != null){
			maxFitnessField.setText(Integer.toString(result.getMaxPossibleFitness()));
		}
		
		Label bestFitness = new Label("Mejor fitness: ");
		TextField bestFitnessField = new TextField(5);
		bestFitnessField.enableInputMethods(false);
		bestFitnessField.setEditable(false);
		if(result != null){
			bestFitnessField.setText(Integer.toString(result.getBestIndividual().getFitnessValue()));
		}
		
		Label avrgFitness = new Label("Promedio fitness: ");
		TextField avrgFitnessField = new TextField(5);
		avrgFitnessField.enableInputMethods(false);
		avrgFitnessField.setEditable(false);
		if(result != null){
			avrgFitnessField.setText(Double.toString(result.getPopulationES()));
		}
		
		Label varFitness = new Label("Varianza fitness: ");
		TextField varFitnessField = new TextField(5);
		varFitnessField.enableInputMethods(false);
		varFitnessField.setEditable(false);
		if(result != null){
			varFitnessField.setText(Double.toString(result.getPopulationVS()));
		}
		
		Label duration = new Label("Duracions: ");
		TextField durationField = new TextField(5);
		durationField.enableInputMethods(false);
		durationField.setEditable(false);
		if(result != null){
			durationField.setText(Long.toString(result.getAlgorithmDuration()));
		}
		
		fieldsPanel.add(maxFitness);
		fieldsPanel.add(maxFitnessField);
		fieldsPanel.add(bestFitness);
		fieldsPanel.add(bestFitnessField);
		fieldsPanel.add(avrgFitness);
		fieldsPanel.add(avrgFitnessField);
		fieldsPanel.add(varFitness);
		fieldsPanel.add(varFitnessField);
		fieldsPanel.add(duration);
		fieldsPanel.add(durationField);
		
		this.add(fieldsPanel, BorderLayout.NORTH);		
	}
}
